<?php

$title = "Panel Logów"; 
require_once("includes/head.php"); if (!$_GET['ajax']) require_once("includes/head_start.php"); 
require_once("languages/".$player -> lang."/admin.php");

if ($player -> rank != "Admin" && $player -> rank != "Staff" && $player -> rank != "Kapitan Gwardii") 
{
    error (NOT_ADMIN);
}


	if ($_POST['view'] == '' && $_POST['view2'] == '')
	{
		$objAmount = $db -> Execute("SELECT count(*) FROM `logs`");
		$intAmount = $objAmount -> fields['count(*)'];
		$objAmount -> Close();
		if (!$intAmount)
		{
			error(NO_LOGS);
		}
		$objLogs = $db -> SelectLimit("SELECT * FROM `logs` order by `czas` ASC", 200);
	}
	if ($_POST['view'] != '' && $_POST['view2'] == '')
	{
		if (!ereg("^[1-9][0-9]*$", $_POST['view']))
		{
			error(ERROR);
		}
		$objAmount = $db -> Execute("SELECT count(*) FROM `logs` WHERE `odbiorca` = ".$_POST['view']." OR `owner` = ".$_POST['view']);
		$intAmount = $objAmount -> fields['count(*)'];
		$objAmount -> Close();
		if (!$intAmount)
		{
			error(NO_LOGS);
		}
		$objLogs = $db -> Execute("SELECT * FROM `logs` WHERE `odbiorca` = ".$_POST['view']." OR `owner` = ".$_POST['view']);
	}
	if ($_POST['view'] != '' && $_POST['view2'] != '')
	{
		if (!ereg("^[1-9][0-9]*$", $_POST['view2']))
		{
			error(ERROR);
		}
		if (!ereg("^[1-9][0-9]*$", $_POST['view']))
		{
			error(ERROR);
		}
		$objAmount = $db -> Execute("SELECT count(*) FROM `logs` WHERE (`odbiorca` = ".$_POST['view']." AND `owner` = ".$_POST['view2'].") OR (`odbiorca` = ".$_POST['view2']." AND `owner` = ".$_POST['view'].")");
		$intAmount = $objAmount -> fields['count(*)'];
		$objAmount -> Close();
		if (!$intAmount)
		{
			error(NO_LOGS);
		}
		$objLogs = $db -> Execute("SELECT * FROM `logs` WHERE (`odbiorca` = ".$_POST['view']." AND `owner` = ".$_POST['view2'].") OR (`odbiorca` = ".$_POST['view2']." AND `owner` = ".$_POST['view'].")");
	}
	if ($_POST['view2'] != '' && $_POST['view'] == '') error(ERROR);


    $arrOwner = array();
    $arrLog = array();
    $arrOdbiorca = array();
    $arrTyp = array();
    $arrCzas = array();
    $i = 0;
    while (!$objLogs -> EOF)
    {
        $arrOwner[$i] = $objLogs -> fields['owner'];
        $arrLog[$i] = $objLogs -> fields['log'];
        $arrOdbiorca[$i] = $objLogs -> fields['odbiorca'];
        $arrTyp[$i] = $objLogs -> fields['typ'];
        $arrCzas[$i] = $objLogs -> fields['czas'];
        $i++;
        $objLogs -> MoveNext();
    }
    $objLogs -> Close();
    $smarty -> assign(array("Logsinfo" => LOGS_INFO,
                            "Lowner" => L_OWNER,
                            "Ltext" => L_TEXT,
                            "Lclear" => L_CLEAR,
                            "Aowner" => $arrOwner,
                            "Alog" => $arrLog,
                            "Aodbiorca" => $arrOdbiorca,
                            "Atyp" => $arrTyp,
                            "Aczas" => $arrCzas));


if ($_GET['step'] == 'clear')
{
    $strDate = $db -> DBDate($newdate);
    $db -> Execute("DELETE FROM `logs` WHERE TIMESTAMPDIFF(DAY, czas, ".$strDate.") > 30") or die($db -> ErrorMsg());
        error('Logi starsze niż miesiąc skasowane!');

//(owner = ".$_POST['view']." OR `odbiorca = ".$_POST['view'].") AND 
}


if (!isset($_GET['step'])) 
{
    $_GET['step'] = '';
}



$smarty -> assign(array("Step" => $_GET['step']));
$smarty -> display('logi.tpl');


if (!$_GET['ajax']) require_once("includes/foot.php");
?>